﻿#if NETFX_CORE || WINDOWS_PHONE

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;

namespace WindowsCommonStorage
{
    internal class NetFxCoreStorageFolder:CommonStorageFolder
    {

        internal StorageFolder _folder;

        internal NetFxCoreStorageFolder()
        {
            this._folder = null;
            
        }

        protected internal override StorageFolder ProtectedEnvironmentFolder
        {
            get 
            { 
                
                return this._folder;
                
            } 
            
        }

        protected internal override string ProtectedName
        {
            get { return this.EnvironmentFolder.Name; }
        }

        internal protected override string ProtectedPath
        {
            get { return this.EnvironmentFolder.Path; }
        }

        protected internal override DateTimeOffset ProtectedDateCreated
        {
            get { return this.EnvironmentFolder.DateCreated; }
        }


        protected internal async override Task ProtectedRenameAsync(string desiredName)
        {
            await this.EnvironmentFolder.RenameAsync(desiredName);
        }

        protected internal async override Task ProtectedRenameAsync(string desiredName, CommonNameCollisionOption option)
        {
            await this.EnvironmentFolder.RenameAsync(desiredName, CommonNameColltionOptionExchanger.Exchange(option));
        }


        protected internal override async Task<CommonStorageFolder> ProtectedCreateFolderAsync(string desiredName)
        {
            return new NetFxCoreStorageFolder { _folder = await this.EnvironmentFolder.CreateFolderAsync(desiredName) };
        }

        protected internal override async Task<CommonStorageFolder> ProtectedCreateFolderAsync(string desiredName, CommonCreationCollisionOption option)
        {
            return new NetFxCoreStorageFolder { _folder = await this.EnvironmentFolder.CreateFolderAsync(desiredName, CommonCreationCollisionOptionExchanger.Exchange(option)) };
        }


        protected internal override async Task<CommonStorageFile> ProtectedCreateFileAsync(string desiredName)
        {
            return new NetFxCoreStorageFile { _file = await this.EnvironmentFolder.CreateFileAsync(desiredName) };
        }

        protected internal override async Task<CommonStorageFile> ProtectedCreateFileAsync(string desiredName, CommonCreationCollisionOption option)
        {
            return new NetFxCoreStorageFile { _file = await this.EnvironmentFolder.CreateFileAsync(desiredName, CommonCreationCollisionOptionExchanger.Exchange(option)) };
        }

        protected internal override async Task ProtectedDeleteAsync()
        {
            await this.EnvironmentFolder.DeleteAsync();
        }



        protected internal override bool ProtectedIsOfType(CommonStorageItemTypes itemType)
        {
            return this.EnvironmentFolder.IsOfType(CommonStorageItemTypesExchanger.Exchange(itemType));
        }



        protected internal override CommonFileAttributes ProtectedAttributes
        {
            get
            {
                return CommonFileAttributesExchanger.Exchange(this.EnvironmentFolder.Attributes);
            }
        }

        protected internal override async Task<IEnumerable<CommonStorageFile>> ProtectedGetFilesAsync()
        {
            List<CommonStorageFile> fileList = new List<CommonStorageFile>();
            foreach (var file in await this.EnvironmentFolder.GetFilesAsync())
            {
                fileList.Add(new NetFxCoreStorageFile { _file = file });
            }
            return fileList;
            
        }

        protected internal override async Task<CommonStorageFolder> ProtectedGetFolderAsync(string name)
        {
            return new NetFxCoreStorageFolder { _folder = await this.EnvironmentFolder.GetFolderAsync(name) };
        }

        protected internal override async Task<IEnumerable<CommonStorageFolder>> ProtectedGetFoldersAsync()
        {
            List<CommonStorageFolder> folders = new List<CommonStorageFolder>();
            foreach (var folder in await this.EnvironmentFolder.GetFoldersAsync())
            {
                folders.Add(new NetFxCoreStorageFolder { _folder = folder });
            }
            return folders;
        }



        protected internal static  async Task<CommonStorageFolder> ProtectedGetFolderFromPathAsync(string path)
        {
            return new NetFxCoreStorageFolder() { _folder = await StorageFolder.GetFolderFromPathAsync(path) };
        }
    }
}
#endif